package excel.imports;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.util.Map;

import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.alibaba.fastjson.JSON;
import com.mvc.core.utils.excel.ExcelUtils;
import com.mvc.core.utils.excel.ImportRspInfo;
import com.mvc.core.utils.excel.ParseSheetCallback;

import excel.export.ProjectEvaluate;



public class MainClass {

	public static void main(String[] args) throws Exception {
		// parseSheet();
		parseSheetWithCallback();
	}

	/**
	 * 解析excel
	 * 
	 * @throws Exception
	 */
	public static void parseSheet() throws Exception {
		Workbook wb = WorkbookFactory.create(new FileInputStream("src\\test\\java\\excel\\imports\\import.xlsx"));
		Sheet sheet = wb.getSheetAt(0);
		// parseSheet
		ImportRspInfo<ProjectEvaluate> list = ExcelUtils.parseSheet(ProjectEvaluate.class, EvaluateVerifyBuilder.getInstance(), sheet, 3, 2);
		System.out.println(JSON.toJSONString(list));
	}

	/**
	 * 解析excel带回调函数：做一些额外字段填充
	 * 
	 * @throws Exception
	 */
	public static void parseSheetWithCallback() throws Exception {
		Workbook wb = WorkbookFactory.create(new FileInputStream("src\\test\\java\\excel\\imports\\import.xlsx"));
		Sheet sheet = wb.getSheetAt(0);
		//获取excel中的所有图片
		Map<String, PictureData> pictures = ExcelUtils.getPictures(0, sheet, wb);
		// parseSheet
		ImportRspInfo<ProjectEvaluate> list = ExcelUtils.parseSheet(ProjectEvaluate.class, EvaluateVerifyBuilder.getInstance(), sheet, 3, 2, new ParseSheetCallback<ProjectEvaluate>() {
			@Override
			public void callback(ProjectEvaluate t, int rowNum) throws Exception {
				// 处理图片
				String pictrueIndex = "0," + rowNum + ",12";
				PictureData remove = pictures.remove(pictrueIndex);
				if (null != remove) {
					byte[] data = remove.getData();
					ByteArrayInputStream in = new ByteArrayInputStream(data);
					System.out.println(in);
				}
			}
		});
		System.out.println(JSON.toJSONString(list));
	}
}
